//
//  ReportView.m
//  deerkids
//
//  Created by zhangxy on 2017/11/22.
//  Copyright © 2017年 wangzhao. All rights reserved.
//

#import "ReportView.h"

#import "MyButton.h"

#import "UIView+Border.h"

#import "ZTextView.h"


@interface ReportView()<UITextViewDelegate>{
    MyButton *checkBtn;
    CGFloat _keyboardHeight;
}

@property(nonatomic,strong) UIViewController *curVC;

@property(nonatomic,strong) UIView *bottomView;
@property(nonatomic,strong) ZTextView *chatTextView;

@end


@implementation ReportView

-(instancetype)initWithShareType:(UIViewController *)vc{
    self = [super initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if (self)
    {
        _curVC = vc;
        self.backgroundColor = [UIColor clearColor];
        
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dissmisMenu)];
        [self addGestureRecognizer:tap];
        
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-280, SCREEN_WIDTH, 280)];
        _bottomView.backgroundColor = UIColorFromRGB(0xF2F2F2);
        [self addSubview:_bottomView];
        
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dissmisMenu)];
        [_bottomView addGestureRecognizer:tap1];
        
        UILabel *labTitle = [UILabel new];
        [labTitle setFrame:CGRectMake(15, 0, SCREEN_WIDTH - 30, 50)];
        [labTitle setFont:[UIFont systemFontOfSize:k12FontSize]];
        [labTitle setTextColor:DEEP_GRAY_COLOR];
        [labTitle setText:@"举报"];
        [_bottomView addSubview:labTitle];
        
        UIImageView *bgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, 1)];
        [bgView1 setBackgroundColor:UIColorFromRGB(0xDBDBDB)];
        [_bottomView addSubview:bgView1];
        [self handleKeyboard];
        [self loadReportList];
        
        
    }
    
    return self;
}
-(void)createLastView:(CGFloat) y{
    [[self chatTextView] setFrame:CGRectMake(15, y + 15, SCREEN_WIDTH-30, 45)];
    y = y + 75;
    UIImageView *bgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, y, SCREEN_WIDTH, 1)];
    [bgView1 setBackgroundColor:UIColorFromRGB(0xDBDBDB)];
    [_bottomView addSubview:bgView1];
    
    UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, y, SCREEN_WIDTH, 64)];
    [bgView setBackgroundColor:UIColor.whiteColor];
    [_bottomView addSubview:bgView];
    
    UIButton *btnClose = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnClose setFrame:CGRectMake(42, y+10, SCREEN_WIDTH - 84, 36)];
    [btnClose setTitle:@"举报" forState:0];
    [btnClose setTitleColor:DEEP_GRAY_COLOR forState:0];
    btnClose.layer.cornerRadius = 18;
    btnClose.layer.masksToBounds = YES;
    btnClose.layer.borderColor = UIColorFromRGB(0xBFBFBF).CGColor;
    btnClose.layer.borderWidth = 1.0f;
    [btnClose addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:btnClose];
    
    y = y +64;
    [_bottomView setFrame:CGRectMake(0, SCREEN_HEIGHT - y, SCREEN_WIDTH, y)];
}

-(void)loadReportList{
//    [NetworkUtil GET:kGetPostRepostReason parameters:@{} success:^(id responseObject) {
//        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
//        if ([dictionary[@"code"] integerValue] == kNetworkSuccessCode) {
//            int i = 1;
//            NSArray *arr = dictionary[@"data"];
//            for (NSDictionary *item in arr) {
//                KnowledgeSort *sort = [KnowledgeSort yy_modelWithJSON:item];
//                [self createShareButton:i type:sort];
//                i ++;
//            }
//
//            [self createLastView:50+(50 * arr.count)];
//        }
//        else{
//            [PopUtil showMessage:dictionary[@"message"] positionType:PopPositionTypeMiddle];
//        }
//    } failure:^(NSError *error) {
//
//    }];
}

-(void)createShareButton:(int) i{
    CGFloat y = 50 + 50*(i-1);
    CGFloat itemW = SCREEN_WIDTH-30;
    CGFloat x = 15;
    MyButton *btn = [MyButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(x, y, itemW, 50)];
    [btn setTitleColor:DEEP_GRAY_COLOR forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:k14FontSize]];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [btn addTarget:self action:@selector(shareWithButton:) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:btn];
    [btn setImage:[UIImage imageNamed:@"btn_anonymous_uncheck"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"btn_anonymous_check"] forState:UIControlStateSelected];
//    [btn setTitle:sort.name forState:UIControlStateNormal];
//    btn.tag = sort.uuid;
    
    [btn addBottomBorderWithColor:BG_GRAY_COLOR andWidth:1];
}

-(ZTextView *)chatTextView{
    if (!_chatTextView) {
        _chatTextView = [[ZTextView alloc] initWithFrame:CGRectMake(15, 10,SCREEN_WIDTH -30, 45)];
        [_chatTextView setPlaceholder:@"输入举报理由"];
        _chatTextView.layer.cornerRadius = 3;
        _chatTextView.layer.masksToBounds = YES;
        if (iOS7) {
            _chatTextView.layoutManager.allowsNonContiguousLayout = NO;
        }
        
        _chatTextView.placeholderColor = LIGHT_GRAY_COLOR;
        _chatTextView.textColor = DEEP_GRAY_COLOR;
        if (iOS7) {
            _chatTextView.layer.borderWidth = 0.75f;
        }else{
            _chatTextView.layer.borderWidth = 0.5f;
        }
        _chatTextView.font = [UIFont systemFontOfSize:16];
        _chatTextView.layer.borderColor = UIColorFromRGB(0xBFBFBF ).CGColor;
        _chatTextView.returnKeyType = UIReturnKeySend;
        _chatTextView.autoresizesSubviews = YES;
        _chatTextView.delegate = self;
        _chatTextView.textAlignment = NSTextAlignmentLeft;
        _chatTextView.autoresizingMask = (UIViewAutoresizingFlexibleWidth);
        [_chatTextView setBackgroundColor:[UIColor whiteColor]];
        [_bottomView addSubview:_chatTextView];
        
    }
    return _chatTextView;
}

-(void)shareWithButton:(MyButton *) shareButton{
    if(checkBtn){
        checkBtn.selected = NO;
    }
    shareButton.selected = YES;
    checkBtn = shareButton;
}


-(void)actionButton:(UIButton *) btn{
    if(checkBtn == nil){
        [PopUtil showMessage:@"请选择原因" positionType:PopPositionTypeMiddle];
        return;
    }
    if(convertToString(_chatTextView.text).length==0){
        [PopUtil showMessage:@"请输入举报内容" positionType:PopPositionTypeMiddle];
        return;
    }
//    [NetworkUtil POST:kPostReport parameters:@{@"id":convertIntToString((int)self.uuid),@"reason_id":convertIntToString((int)checkBtn.tag),@"content":convertToString(_chatTextView.text)} success:^(id responseObject) {
//        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
//        if ([dictionary[@"code"] integerValue] == kNetworkSuccessCode) {
//            [PopUtil showMessage:@"举报成功!" positionType:PopPositionTypeMiddle];
//            [self hideKeyboard];
//            [self dissmisMenu];
//        }
//        else{
//            [PopUtil showMessage:dictionary[@"message"] positionType:PopPositionTypeMiddle];
//        }
//    } failure:^(NSError *error) {
//
//    }];
    
}


- (void)dissmisMenu{
    if(_keyboardHeight>0){
        [self hideKeyboard];
        return;
    }
    
    CGRect sheetViewF = self.frame;
    sheetViewF.origin.y = SCREEN_HEIGHT;
    
    
    [UIView animateWithDuration:0.2 animations:^{
        
        self.frame = sheetViewF;
        self.alpha = 0.0;
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)show{
    CGRect sheetViewF = self.frame;
    sheetViewF.origin.y = 0;
    
    [UIView animateWithDuration:0.2 animations:^{
        self.frame = sheetViewF;
    } completion:^(BOOL finished) {
        
    }];
}



- (void)handleKeyboard{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

-(void)removeKeyboardObserver{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}


//键盘显示
- (void)keyboardWillShow:(NSNotification *)notification {
    float animationDuration = [[[notification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    _keyboardHeight = [[[notification userInfo] objectForKey:@"UIKeyboardBoundsUserInfoKey"] CGRectValue].size.height;
    
    
    NSNumber *curve = [notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationCurve:[curve intValue]];
    [UIView setAnimationDelegate:self];
    // get a rect for the view frame
    
    {
        CGRect bf         = _bottomView.frame;
        
        
        
       bf.origin.y =  (SCREEN_HEIGHT - bf.size.height) - _keyboardHeight;
       _bottomView.frame = bf;
    }
    
    // commit animations
    [UIView commitAnimations];
}


#pragma mark 键盘隐藏 keyboard notification
-(void)hideKeyboard{
    [_chatTextView resignFirstResponder];
}
//键盘隐藏
- (void)keyboardWillHide:(NSNotification *)notification {
    float animationDuration = [[[notification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    [UIView beginAnimations:@"bottomBarDown" context:nil];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
    _keyboardHeight = 0;
    
    [UIView commitAnimations];
    
    [UIView animateWithDuration:0.25 animations:^{
        CGRect bf = _bottomView.frame;
        bf.origin.y = (SCREEN_HEIGHT - bf.size.height);
        _bottomView.frame = bf;
        
    }];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
